<template>
    <panel-head :route="route" />
    <el-table :data="tableData.list" style="width: 100%">
        <el-table-column prop="id" label="id" />
        <el-table-column prop="name" label="昵称" />
        <el-table-column prop="permissions_id" label="所属组别">
            <template #default="scope">
                {{ getPermissionName(scope.row.permissions_id) }}
            </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" />
        <el-table-column prop="active" label="状态">
            <template #default="scope">
                <el-tag :type="scope.row.active ? 'success' : 'danger'">{{ scope.row.active ? '正常' : '失效' }}</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
            <template #default="scope">
                <div class="flex-box">
                    <el-icon><Clock /></el-icon>
                    <span style="margin-left: 10px;">{{ scope.row.create_time }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" type="primary" @click="open(scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination-info">
        <el-pagination
            v-model:current-page="pagination.pageNum"
            :page-size="pagination.pageSize"
            :background="false"
            size="small"
            layout="total, prev, pager, next"
            :total="tableData.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
    <el-dialog
        v-model="dialogFormVisible"
        :before-close="beforeClose"
        title="编辑用户"
        width="500px"
    >
        <el-form 
            ref="formRef"
            :rules="rules"
            :model="formData"
            label-width="100px"
            label-position="left"
        >
            <el-form-item label="手机号" prop="mobile">
                <el-input v-model="formData.mobile" disabled />
            </el-form-item>
            <el-form-item label="昵称" prop="name">
                <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="菜单权限" prop="permissions_id">
                <el-select v-model="formData.permissions_id" placeholder="请选择菜单权限" style="width: 240px;">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div>
                <el-button type="primary" @click="submitForm(formRef)">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { getAccountList, menuSelectList, updateUserInfo } from '@/api'
import { ref, reactive, onMounted } from 'vue'
import dayjs from 'dayjs'
import { useRoute } from 'vue-router'

const route = useRoute()
const formRef = ref(null)
// 是否显示对话框
const dialogFormVisible = ref(false)
// 表单数据
const formData = reactive({
    mobile: '',
    name: '',
    permissions_id: ''
})
const rules = reactive({
    name: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
    permissions_id: [{ required: true, message: '请选择菜单权限', trigger: 'change' }]
})

const pagination = reactive({
    pageNum: 1,
    pageSize: 10
})
const tableData = reactive({
    list: [],
    total: 0,
})

// 获取账号列表，封装成一个函数
const getAccountData = () => {
    getAccountList(pagination).then(({ data }) =>{
        // data.code === -2 时，token失效，需要重新登陆
        const { list, total } = data.data
        list.forEach(item => {
            item.create_time = dayjs(item.create_time).format('YYYY-MM-DD')
        })
        tableData.list = list
        tableData.total = total
    })
}
// 菜单权限下拉身份数据
let options = ref([])
onMounted(() => {
    getAccountData()
    menuSelectList().then(({ data }) => {
        options.value = data.data
    })
})
// permissions_id对应options中的name
const getPermissionName = (id) => {
    const res = options.value.find(item => item.id === id)
    return res ? res.name : '超级管理员'
}

// 打开弹框
const open = (rowData) => {
    dialogFormVisible.value = true
    // 一个弹框，无需使用nextTick，直接赋值即可
    Object.assign(formData, { mobile: rowData.mobile, name: rowData.name, permissions_id: rowData.permissions_id })
}
// 提交表单
const submitForm = async (formEl) => {
    if (!formEl) return 
    await formEl.validate((valid, fields) => {
        if (valid) {
            // 提交表单修改用户信息
            updateUserInfo({ name: formData.name, permissions_id: formData.permissions_id }).then(({ data }) => {
                if (data.code === 10000) {
                    // 关闭弹窗，更新列表数据
                    dialogFormVisible.value = false
                    getAccountData()
                }
            })
        } else {
            console.log('错误提交')
        }
    })
}
const handleSizeChange = (val) => {
    pagination.pageSize = val
    getAccountData()

}
const handleCurrentChange = (val) => {
    pagination.pageNum = val
    getAccountData()
}
</script>

<style lang="less" scoped>
.flex-box {
    display: flex;
    align-items: center;
}
</style>